cmake_minimum_required (VERSION 2.6)
project (wxgisgdallib)

set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configs" FORCE)
set(CMAKE_COLOR_MAKEFILE ON)

# parse the version number from gdal_version.h and include in GDAL_MAJOR_VERSION and GDAL_MINOR_VERSION
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdal_version.h GDAL_VERSION_H_CONTENTS)
string(REGEX MATCH "GDAL_VERSION_MAJOR[ \t]+([0-9]+)"
  GDAL_MAJOR_VERSION ${GDAL_VERSION_H_CONTENTS})
string (REGEX MATCH "([0-9]+)"
  GDAL_MAJOR_VERSION ${GDAL_MAJOR_VERSION})
string(REGEX MATCH "GDAL_VERSION_MINOR[ \t]+([0-9]+)"
  GDAL_MINOR_VERSION ${GDAL_VERSION_H_CONTENTS})
string (REGEX MATCH "([0-9]+)"
  GDAL_MINOR_VERSION ${GDAL_MINOR_VERSION})
string(REGEX MATCH "GDAL_VERSION_REV[ \t]+([0-9]+)"
  GDAL_REV_VERSION ${GDAL_VERSION_H_CONTENTS})  
string (REGEX MATCH "([0-9]+)"
  GDAL_REV_VERSION ${GDAL_REV_VERSION})
    
# Setup package meta-data
set(GDAL_VERSION ${GDAL_MAJOR_VERSION}.${GDAL_MINOR_VERSION}.${GDAL_REV_VERSION})
message(STATUS "gdal version=[${GDAL_VERSION}]")
    
set(GDAL_LIB_NAME wxgisgdal${GDAL_MAJOR_VERSION}${GDAL_MINOR_VERSION})
message(STATUS "GDAL lib name ... " ${GDAL_LIB_NAME})
 
#PROJECTS_DEPENDENCIES_DIR can be used to specify a single base
#folder where the required dependencies may be found.
# set(PROJECTS_DEPENDENCIES_DIR "Dependencies" CACHE PATH "Path to prebuilt  dependencies")
# set(PROJECTS_DEP_SEARCH_PATH 
    # ${PROJECTS_DEPENDENCIES_DIR}
    # $ENV{PROJECTS}
# )

# message(STATUS "Search path: ${PROJECTS_DEP_SEARCH_PATH}")
#Set hardcoded path guesses for various platforms
# if (UNIX)
  # set(PROJECTS_DEP_SEARCH_PATH ${PROJECTS_DEP_SEARCH_PATH} /usr/local)
# endif ()
#give guesses as hints to the find_package calls
# set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${PROJECTS_DEP_SEARCH_PATH})
# set(CMAKE_FRAMEWORK_PATH ${CMAKE_FRAMEWORK_PATH} ${PROJECTS_DEP_SEARCH_PATH})

#diagnosing info
message(STATUS "c++ compiler ... " ${CMAKE_CXX_COMPILER})

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/gcore)

if(WIN32)
    set(LIB_INSTALL_PATH ${CMAKE_CURRENT_BINARY_DIR}/lib/)
elseif(UNIX)
    set(LIB_INSTALL_PATH lib/wxgis)
endif()

# uninstall target
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY)

# set path to additional CMake modules
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})

# needed packages
find_package(ZLIB REQUIRED)
if(ZLIB_FOUND)
    include_directories(${ZLIB_INCLUDE_DIRS})
    #link_directories(${ZLIB_LIBRARIES})
    add_definitions(-DHAVE_ZLIB_H -DHAVE_ZLIB -DHAVE_LIBZ)
    if(MSVC)
        add_definitions(-DZLIB_DLL)
    endif(MSVC)
endif(ZLIB_FOUND)

find_package(Boost REQUIRED)    #COMPONENTS system thread regex program
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_definitions(-DHAVE_BOOST)
endif(Boost_FOUND)

find_package(CURL REQUIRED)
if(CURL_FOUND)
    include_directories(${CURL_INCLUDE_DIRS})
    add_definitions(-DHAVE_CURL)
endif(CURL_FOUND)
    
if(MSVC)
  option(BUILD_RELEASE_DEBUG_DIRS "Set OFF to build each co# uninstall target
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY)nfiguration to a separate directory" ON)
  mark_as_advanced(BUILD_RELEASE_DEBUG_DIRS)
endif()

# For windows, do not allow the compiler to use default target (Vista).
if(WIN32)
  add_definitions(-D_WIN32_WINNT=0x0501 -D_USRDLL)
endif(WIN32)

if(MSVC)
  set(CMAKE_DEBUG_POSTFIX "d")
  add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
  add_definitions(-D_MBCS)
  set(GDAL_CSOURCES ${GDAL_CSOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/gcore/Version.rc)
  source_group("Resource Files" FILES ${CMAKE_CURRENT_SOURCE_DIR}/gcore/Version.rc)  
endif(MSVC)

set(GDAL_HHEADERS ${GDAL_HHEADERS}
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdal_version.h
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdal.h
)

set(GDAL_CSOURCES ${GDAL_CSOURCES}
    ${CMAKE_CURRENT_SOURCE_DIR}/gcore/gdal_misc.cpp
 )

#/D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "STRICT" /D "_USRDLL" /D "_WINDLL" /D "_MBCS"-DCHANGE_BY_BISHOP
    
add_definitions(-DSTRICT)

option(GDALLIB_BUILD_OGR "Build ogr lib" YES)
option(GDALLIB_BUILD_GDAL "Build gdal lib" YES)
    
include(cpl) # uninstall target
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY)
if(GDALLIB_BUILD_OGR)   
    include(ogr)
endif(GDALLIB_BUILD_OGR)
if(GDALLIB_BUILD_GDAL)
    include(gdal)
endif(GDALLIB_BUILD_GDAL)    

if(GDALLIB_BUILD_OGR AND NOT GDALLIB_BUILD_GDAL)
    add_definitions(-DOGR_ONLY)
endif()

add_library(${GDAL_LIB_NAME} SHARED ${GDAL_HHEADERS} ${GDAL_CSOURCES})
add_custom_target(uninstall
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)

if(WIN32)
  # Postfix of DLLs:
  set(LIB_DEBUG_POSTFIX d)
else()
  # Postfix of so's:
  set(LIB_DEBUG_POSTFIX)
endif()

set_target_properties(${GDAL_LIB_NAME}
	PROPERTIES OUTPUT_NAME "${GDAL_LIB_NAME}"
	DEBUG_POSTFIX "${LIB_DEBUG_POSTFIX}"
	ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib
	LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib
    	RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib
	)
    
if(ZLIB_FOUND)
    target_link_libraries(${GDAL_LIB_NAME} ${ZLIB_LIBRARIES})
endif(ZLIB_FOUND)
if(CURL_FOUND)
    target_link_libraries(${GDAL_LIB_NAME} ${CURL_LIBRARIES})
endif(CURL_FOUND)
if(PROJ_FOUND)
    target_link_libraries(${GDAL_LIB_NAME} ${PROJ_LIBRARY})
endif(PROJ_FOUND)
if(GEOS_FOUND)
    target_link_libraries(${GDAL_LIB_NAME} ${GEOS_LIBRARY})
endif(GEOS_FOUND)
if(EXPAT_FOUND)
    target_link_libraries(${GDAL_LIB_NAME} ${EXPAT_LIBRARIES})
endif(EXPAT_FOUND)
if(POSTGRES_FOUND)
    target_link_libraries(${GDAL_LIB_NAME} ${POSTGRES_LIBRARY})
    if(WIN32)    
        target_link_libraries(${GDAL_LIB_NAME} shfolder.lib wsock32.lib ws2_32.lib secur32.lib ) 
    endif()
endif(POSTGRES_FOUND)
if(TIFF_FOUND)
    target_link_libraries(${GDAL_LIB_NAME} ${TIFF_LIBRARIES})
endif(TIFF_FOUND)
if(JPEG_FOUND)
    target_link_libraries(${GDAL_LIB_NAME} ${JPEG_LIBRARIES})
endif(JPEG_FOUND)
if(PNG_FOUND)
    target_link_libraries(${GDAL_LIB_NAME} ${PNG_LIBRARIES})
endif(PNG_FOUND)
if(LIBKM_FOUND)
    target_link_libraries(${GDAL_LIB_NAME} ${LIBKM_LIBRARY})
endif(LIBKM_FOUND)

if(UNIX)
  target_link_libraries(${GDAL_LIB_NAME} dl)
endif()

target_link_libraries(${GDAL_LIB_NAME} ${LIBKML_LIB_PATH})

if(ENABLE_SOLUTION_FOLDERS)
	set_target_properties(${GDAL_LIB_NAME} PROPERTIES FOLDER "wxgisgdal")
endif()	

set_target_properties(${GDAL_LIB_NAME} PROPERTIES PROJECT_LABEL wxgisgdal)
set_target_properties(${GDAL_LIB_NAME} PROPERTIES VERSION ${GDAL_MAJOR_VERSION}.${GDAL_MINOR_VERSION})
set_target_properties(${GDAL_LIB_NAME} PROPERTIES SOVERSION ${GDAL_REV_VERSION})

if(WIN32)
install(TARGETS ${GDAL_LIB_NAME} DESTINATION ${LIB_INSTALL_PATH})
else()
install(TARGETS ${GDAL_LIB_NAME}
        RUNTIME DESTINATION bin/wxgis
        ARCHIVE DESTINATION lib/wxgis
        LIBRARY DESTINATION lib/wxgis )
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/alg DESTINATION install/gdal FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/gcore DESTINATION install/gdal FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")    
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ogr DESTINATION install/gdal FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")    
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/port DESTINATION install/gdal FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")    
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/frmts DESTINATION install/gdal FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")
endif()  
    
